/* js for mark page */

(function() {
    window.onload = function() {
      var placeMarkers = [];
      var map, selfMarker, options, center, lastZoomLevel;
      var homeLatitude, homeLongitude, homeLatLng;
      var lastLatitude, lastLongitude, lastLatLng;

      function autoFill(results) {
        //console.log(results[0]);
        $('#id_address').css({'color':'#1a1a1a'});

        var country, region, city, district, plate, address,
        formatted_address = results[0].formatted_address,
        address_components = results[0].address_components;
        ///var address = address_components[1].long_name + address_components[0].long_name;

        //console.log(results[0]);
        ///var address = address_components[1].long_name + address_components[0].long_name;

        //document.getElementById('id_address').value = formatted_address;
        //if (address_components.length >= 6) {
        if (results[0].geometry.location_type === 'ROOFTOP') {
          country = address_components[5].long_name;
          region = address_components[4].long_name;
          city = address_components[3].long_name;
          district = address_components[2].long_name;
          plate = address_components[1].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;

          //document.getElementById('id_country').value = address_components[5].long_name;
          //document.getElementById('id_region').value = address_components[4].long_name;
          //document.getElementById('id_city').value = address_components[3].long_name;
          //document.getElementById('id_district').value = address_components[2].long_name;
          //} else if (address_components.length == 5) {
        } else if (results[0].geometry.location_type === 'RANGE_INTERPOLATED') {
          country = address_components[5].long_name;
          region = address_components[4].long_name;
          city = address_components[3].long_name;
          district = address_components[2].long_name;
          plate = address_components[1].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;    
          //document.getElementById('id_country').value = address_components[5].long_name;
          //document.getElementById('id_region').value = address_components[4].long_name;
          //document.getElementById('id_city').value = address_components[3].long_name;
          //document.getElementById('id_district').value = address_components[2].long_name;
        } else if (results[0].geometry.location_type === 'GEOMETRIC_CENTER') {
          country = address_components[4].long_name;
          region = address_components[3].long_name;
          city = address_components[2].long_name;
          district = address_components[0].long_name;
          plate = address_components[1].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;    
          //document.getElementById('id_country').value = address_components[4].long_name;
          //document.getElementById('id_region').value = address_components[3].long_name;
          //document.getElementById('id_city').value = address_components[2].long_name;
          //document.getElementById('id_district').value = address_components[1].long_name;
        } else if (results[0].geometry.location_type === 'APPROXIMATE') {
          country = address_components[4].long_name;
          region = address_components[3].long_name;
          city = address_components[2].long_name;
          district = address_components[1].long_name;
          plate = address_components[0].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;    
          //document.getElementById('id_country').value = address_components[4].long_name;
          //document.getElementById('id_region').value = address_components[3].long_name;
          //document.getElementById('id_city').value = address_components[2].long_name;
          //document.getElementById('id_district').value = address_components[1].long_name;
        }               

        /*
        //document.getElementById('id_address').value = formatted_address;
        if (address_components.length >= 6) {
          country = address_components[5].long_name || '';
          region = address_components[4].long_name || '';
          city = address_components[3].long_name || '';
          district = address_components[2].long_name || '';
          plate = address_components[1].long_name || '';

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;

          document.getElementById('id_address').value = country + region + city + district + plate;
          //document.getElementById('id_country').value = address_components[5].long_name;
          //document.getElementById('id_region').value = address_components[4].long_name;
          //document.getElementById('id_city').value = address_components[3].long_name;
          //document.getElementById('id_district').value = address_components[2].long_name;
          //document.getElementById('id_address').value = address_components[5].long_name + address_components[4].long_name + address_components[3].long_name[3] + address_components[2].long_name + address_components[1].long_name;
        } else if (address_components.length == 5) {
          document.getElementById('id_country').value = address_components[4].long_name;
          document.getElementById('id_region').value = address_components[3].long_name;
          document.getElementById('id_city').value = address_components[2].long_name;
          document.getElementById('id_district').value = address_components[1].long_name;

          document.getElementById('id_address').value = formatted_address; 
        }*/                                                                                      

        

        //console.log(document.getElementById('id_country').value);
        //console.log(document.getElementById('id_city').value);
        // Unbind to click event
        //$('id_address').unbind('click');
      }

      function autoMove(results) {
        var country, region, city, district, plate, address,
            latitude, longitude, latLng,
            address_components = results[0].address_components,
            formatted_address = results[0].formatted_address;


        selfMarker.setPosition(latLng);
        map.setCenter(latLng);

        //console.log(results[0]);
        $('#id_address').css({'color':'#1a1a1a'});
        ///var address = address_components[1].long_name + address_components[0].long_name;

        //document.getElementById('id_address').value = formatted_address;
        //if (address_components.length >= 6) {
        if (results[0].geometry.location_type === 'ROOFTOP') {
          country = address_components[5].long_name;
          region = address_components[4].long_name;
          city = address_components[3].long_name;
          district = address_components[2].long_name;
          plate = address_components[1].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;

          //document.getElementById('id_country').value = address_components[5].long_name;
          //document.getElementById('id_region').value = address_components[4].long_name;
          //document.getElementById('id_city').value = address_components[3].long_name;
          //document.getElementById('id_district').value = address_components[2].long_name;
          //} else if (address_components.length == 5) {
        } else if (results[0].geometry.location_type === 'RANGE_INTERPOLATED') {
          country = address_components[5].long_name;
          region = address_components[4].long_name;
          city = address_components[3].long_name;
          district = address_components[2].long_name;
          plate = address_components[1].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;    
          //document.getElementById('id_country').value = address_components[5].long_name;
          //document.getElementById('id_region').value = address_components[4].long_name;
          //document.getElementById('id_city').value = address_components[3].long_name;
          //document.getElementById('id_district').value = address_components[2].long_name;
        } else if (results[0].geometry.location_type === 'GEOMETRIC_CENTER') {
          country = address_components[4].long_name;
          region = address_components[3].long_name;
          city = address_components[2].long_name;
          district = address_components[0].long_name;
          plate = address_components[1].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;    
          //document.getElementById('id_country').value = address_components[4].long_name;
          //document.getElementById('id_region').value = address_components[3].long_name;
          //document.getElementById('id_city').value = address_components[2].long_name;
          //document.getElementById('id_district').value = address_components[1].long_name;
        } else if (results[0].geometry.location_type === 'APPROXIMATE') {
          country = address_components[4].long_name;
          region = address_components[3].long_name;
          city = address_components[2].long_name;
          district = address_components[1].long_name;
          plate = address_components[0].long_name;

          document.getElementById('id_country').value = country;
          document.getElementById('id_region').value = region;
          document.getElementById('id_city').value = city;
          document.getElementById('id_district').value = district;
          document.getElementById('id_address').value = country + region + city + district + plate;    
          //document.getElementById('id_country').value = address_components[4].long_name;
          //document.getElementById('id_region').value = address_components[3].long_name;
          //document.getElementById('id_city').value = address_components[2].long_name;
          //document.getElementById('id_district').value = address_components[1].long_name;
        }                                                                                                                                                                                                                               

        //document.getElementById('id_address').value = formatted_address;
        /*
        if (results[0].geometry.location_type === 'APPROXIMATE') {
          if (results[0].geometry.location.Ja && results[0].geometry.location.Ka) {
            latitude  = results[0].geometry.location.Ja;
            longitude = results[0].geometry.location.Ka;
          } else if (results[0].geometry.location.Ka && results[0].geometry.location.La) {
            latitude  = results[0].geometry.location.Ka;
            longitude = results[0].geometry.location.La;      
          }
        } else if (results[0].geometry.location_type === 'ROOFTOP') {
            latitude  = results[0].geometry.location.Ja;
            longitude = results[0].geometry.location.Ka;
        } else if (results[0].geometry.location_type === 'GEOMETRIC_CENTER') {
            latitude  = results[0].geometry.location.Ja;
            longitude = results[0].geometry.location.Ka;           
        } else {
          if (results[0].geometry.location.Ja && results[0].geometry.location.Ka) {
            latitude  = results[0].geometry.location.Ja;
            longitude = results[0].geometry.location.Ka;
          } else if (results[0].geometry.location.Ka && results[0].geometry.location.La) {
            latitude  = results[0].geometry.location.Ka;
            longitude = results[0].geometry.location.La;      
          } else if (results[0].geometry.location.Ia && results[0].geometry.location.Ja) {                                                      
            latitude  = results[0].geometry.location.Ia;
            longitude = results[0].geometry.location.Ja;
          }
        }*/
        latitude  = results[0].geometry.location.lat();
        longitude = results[0].geometry.location.lng();
        utils.writeLatLng(latitude, longitude);

        latLng    = new google.maps.LatLng(latitude, longitude);
        selfMarker.setPosition(latLng);
        map.setCenter(latLng);                  
      }

      homeLatitude = utils.readCookie('homeLatitude');
      homeLongitude = utils.readCookie('homeLongitude');
      homeLatLng = new google.maps.LatLng(homeLatitude, homeLongitude);

      lastLatitude = utils.readCookie('lastLatitude') || homeLatitude;
      lastLongitude = utils.readCookie('lastLongitude') || homeLongitude;
      lastLatLng = new google.maps.LatLng(lastLatitude, lastLongitude);

      center = lastLatLng || homeLatLng;
      lastZoomLevel = parseInt(utils.readCookie('lastZoomLevel'), 10) || 10;

      options = {
        center: center,
        zoom: lastZoomLevel,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        navigationControl: true,
        navigationControlOptions: {
          position: google.maps.ControlPosition.RIGHT_BOTTOM,
          style: google.maps.NavigationControlStyle.SMALL
        }
      };

      map = new google.maps.Map(document.getElementById('map-mark'), options);

      var control = document.createElement('div');
      //control.id = 'button-home';
      //control.style.position = 'absolute';
      //control.style.top = '10px';
      //control.style.right = '10px';
      control.innerHTML = '<img src="/static/img/map/home.png" width="50" height="50" />';
      //control.index = 1;

      // Add the control to the map
      map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);


      /*
      * Events
      */
      // When the button is clicked pan to sydney
      google.maps.event.addDomListener(control, 'click', function() {
          console.log('back to home');
          map.setCenter(homeLatLng);
          selfMarker.setPosition(homeLatLng);
          lastLatitude = homeLatitude;
          lastLongitude = homeLongitude;
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);    
          //map.panTo(homeLatLng);
      });

      // Change the style when the user mousedowns
      //google.maps.event.addDomListener(control, 'mousedown', function() {
      //    control.className = 'button-home';
      //});

      // Change the style back when the user mouseups
      //google.maps.event.addDomListener(control, 'mouseup', function() {
      //    control.className = 'button-home';
      //});

      google.maps.event.addListener(map, 'zoom_changed', function() {
          var zoomLevel = map.getZoom();
          utils.createCookie('lastZoomLevel', zoomLevel, 365);
          console.log('lastZoomLevel: ' + zoomLevel);
      });

      selfMarker = gmap.createSelfMarker(map, lastLatLng || homeLatLng);                             
      utils.writeLatLng(lastLatitude || homeLatitude, lastLongitude || homeLongitude);

      console.log('map has been created!');
      google.maps.event.addListener(selfMarker, 'dragend', function() {
          selfMarker.setOptions({icon: gmap.selfMarker});
          var lastLatLng = selfMarker.getPosition();
          var expires = new Date(Date.now() + gmap.ONE_DAY * 365).toUTCString();
          lastLatitude = lastLatLng.lat();
          lastLongitude = lastLatLng.lng();
          console.log('lastLatidue: ' + lastLatitude);
          console.log('lastLongitude: ' + lastLongitude);
          console.log('start set cookie');
          utils.createCookie('lastLatitude', lastLatitude);
          utils.createCookie('lastLongitude', lastLongitude);
          console.log(lastLatitude);
          console.log(lastLongitude);
          console.log('end set cookie');
          map.setCenter(lastLatLng);

          utils.writeLatLng(lastLatitude, lastLongitude);

          // If the 'from location to addrss' radio button is selected,
          // then call geocode
          if ($('input#from-location-to-address:checked').val()) {
            gmap.geocode({'latLng':lastLatLng}, autoFill);
          }
      });                                                                               

      $('input#from-location-to-address').change(function() {
          $('span#from-location-to-address-text').css({'color':'#000'});
          $('span#from-address-to-location-text').css({'color': '#ccc'});
      });

      $('input#from-address-to-location').change(function() {
          $('span#from-address-to-location-text').css({'color': '#000'});
          $('span#from-location-to-address-text').css({'color': '#ccc'});  
      });

      // From address get location information
      $('input#id_address').change(function() {
        //setTimeout(function() {
          var address = $(this).val();
          console.log(address);
          if ($('input#from-address-to-location:checked').val()) {
            gmap.geocode({'address':address}, autoMove);
          }
         // }, 800);
      });

      $('textarea').val('说点什么').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});

          $(this).unbind('click');
      });

      $('input#id_name').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});

          $(this).unbind('click'); 
      });

      $('input#id_address').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});

          $(this).unbind('click'); 
      });

      $('input#id_tags').click(function() {
          $(this).val('');
          $(this).css({'color':'#1a1a1a'});

          $(this).unbind('click'); 
      });                               

    };
})();
